package StudentPractice;

import java.util.*;

/*
 *文件名: main
 *创建者:
 *创建时间:2025/5/21 16:42
 *描述: private String name;
    private int age;
    private long id;
    private String address;
 */
public class main {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        select(list);
        }



    public static void select(List<Student> list){
        System.out.println("-------欢迎使用学生管理系统-------");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查询学生");
        System.out.println("5.退出系统");
        System.out.println("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n){
            case 1:add(list);
                break;
            case 2:delete(list);
                break;
            case 3:
                revise(list);
                break;
            case 4:search(list);
                break;
            case 5:
            {System.out.println("感谢您的使用，再见！");
                return;}
    }
    select(list);
    }

    //添加学生信息
    public static void add(List<Student> list){
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入学生学号：");
        long id = sc.nextLong();
        for(Student student : list){
            if (student.getId() == id){
                System.out.println("该学号已存在，信息为：");
                System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
                System.out.println(student.getId()+"\t\t"+student.getName()+"\t\t"+student.getAge()+"岁"+"\t\t"+student.getAddress());
                return;
            }
        }

        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生居住地：");
        String address = sc.next();
        list.add(new Student(id, name, age, address));
        System.out.println("添加成功");
        return;
    }

    //查找选择以及查找全部学生
    public static void search(List<Student> list){
        System.out.println("1.查看所有学生");
        System.out.println("2.查找学生");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");//空5格
        switch (n){
            case 1:
                for(Student student : list){
                    System.out.println(student.getId()+"\t\t"+student.getName()+"\t\t"+student.getAge()+"岁"+"\t\t"+student.getAddress());
                }
                break;
                case 2:
                    searchSingle(list);
        }
        return;

    }

    //查找单个学生
    public static void searchSingle(List<Student> list){
        Scanner sc = new Scanner(System.in);


        System.out.println("请输入查找学生学号：");

        Scanner sc1 = new Scanner(System.in);

        long id = sc.nextLong();
        for(Student student : list){
            if (student.getId()==id ){
                System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
                System.out.println(student.getId()+"\t\t"+student.getName()+"\t\t"+student.getAge()+"岁"+"\t\t"+student.getAddress());
                    }
                    else System.out.println("该信息不存在");
                       // searchSingle(list);}
                }
        }


    //删除学生信息
    public static void delete(List<Student> list){
            Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生学号");
        long id = sc.nextLong();
        for(Student student : list){
            if (student.getId()==id ){
                list.remove(student);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("该信息不存在");
        //  delete(list);}
    }


    //修改学生信息
    public static void revise(List<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号");
        long id = sc.nextLong();
        int i=0;
        for(Student student : list){
            if (student.getId() == id){
                i=i+1;
            }
        }
        if (i==0){
        System.out.println("该学号不存在");
        return;}
        System.out.println("请输入您要修改的该学生的信息:");
        System.out.println("请输入修改后学生学号：");
        long d = sc.nextLong();
        for(Student student : list){
            if (student.getId() == d){
                System.out.println("该学号已存在，信息为：");
                System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
                System.out.println(student.getId()+"\t\t"+student.getName()+"\t\t"+student.getAge()+"岁"+"\t\t"+student.getAddress());
                return;
            }
        }
        System.out.println("请输入修改后学生姓名:");
        String name = sc.next();
        System.out.println("请输入修改后学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入修改后学生居住地：");
        String address = sc.next();
        int x=0;
        for(Student student : list){
            if (student.getId()== id){
                student=list.get(x);
                student.setId(d);
                student.setAge(age);
                student.setName(name);
                student.setAddress(address);
                System.out.println("修改成功");
                break;
            }
            x++;
        }
    }
}

